<?php

class adminCategories extends Controller
{
    public function index()
    {
    	// Verify if admin is logge in, otherwise redirect out
    	Helper::verifyAdminLoggedIn();
    	
	    $categories = $this->model->getAllCategories();
		//print "Categories Are : <pre>" . print_r($categories,true) . "</pre>";exit;
        $amount_of_categories = $this->model->getAmountOfCategories();
     
        // load views
        require APP . 'views/admin_templates/header.php';
        require APP . 'views/adminCategories/index.php';
        require APP . 'views/admin_templates/footer.php';
    }
 
	public function addCategory()
    {
    	// Verify if admin is logge in, otherwise redirect out
    	Helper::verifyAdminLoggedIn();

        if (isset($_POST["submit_add_category"])) {
            $this->model->addCategory($_POST["name_en"],$_POST["name_ar"]);
        }

        
        header('location: ' . URL . 'adminCategories');
    }

    public function deleteCategory($category_id)
    {
    	// Verify if admin is logge in, otherwise redirect out
    	Helper::verifyAdminLoggedIn();
    	
        if (isset($category_id)) {
            $this->model->deleteCategory($category_id);
        }

        header('location: ' . URL . 'adminCategories');
    }

    public function editCategory($category_id)
    {
    	// Verify if admin is logge in, otherwise redirect out
    	Helper::verifyAdminLoggedIn();

        if (isset($category_id)) {
            $category = $this->model->getCategory($category_id);

            require APP . 'views/admin_templates/header.php';
	        require APP . 'views/adminCategories/edit.php';
	        require APP . 'views/admin_templates/footer.php';
            
        } else {
            header('location: ' . URL . 'adminCategories');
        }
    }
    
   
    public function updateCategory()
    {
    	// Verify if admin is logge in, otherwise redirect out
    	Helper::verifyAdminLoggedIn();

        if (isset($_POST["submit_update_category"])) {
            $this->model->updateCategory($_POST["name_en"],$_POST["name_ar"],$_POST["category_id"]);
        }
        
        header('location: ' . URL . 'adminCategories/index');
    }

    public function ajaxGetStats()
    {
        $amount_of_categories = $this->model->getAmountOfCategories();

        // simply echo out something. A supersimple API would be possible by echoing JSON here
        echo $amount_of_categories;
    } 
}
